﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using DocumentManagement.Views;
using DocumentManagement.Models;

namespace DocumentManagement.ViewsModels
{
  public  class LoginWindowViewsModel : ViewsModelBase
    {
        //public string TextUsername { get; set; }
        //public string TextPassWord { get; set; }

        private string _textUsername;
        public string TextUsername
        {
            get { return _textUsername; }
            set
            {
                if (_textUsername != value)
                {
                    _textUsername = value;
                    OnPropertyChanged("TextUsername");
                }
            }
        }
        private string _textPassWord;
        public string TextPassWord
        {
            get { return _textPassWord; }
            set
            {
                if (_textPassWord != value)
                {
                    _textPassWord = value;
                    OnPropertyChanged("TextPassWord");
                }
            }
        }

      public bool AuthenticateUser(string domainName, string userName, string password)
      {
          bool ret = false;

          try
          {
              DirectoryEntry de = new DirectoryEntry("LDAP://" + domainName, userName, password);
              DirectorySearcher dsearch = new DirectorySearcher(de);
              SearchResult results = null;

              results = dsearch.FindOne();

              ret = true;
          }
          catch
          {
              ret = false;
          }

          return ret;
      }
      public void CheckUser()
      {
          bool CheckLogin = AuthenticateUser("qshc.net", TextUsername.ToUpper(), TextPassWord);
          // MessageBox.Show(aa.ToString());
          if (CheckLogin == true)
          {
              
              var entityModels = new QSHCEntities1();
             
              CurrentEmployee = entityModels.Employees.Single(p => p.EmployeeCode == TextUsername.ToUpper());
              //if (CurrentEmployee != null)
              //{
              //    //IDEmployee = entityModels.Employees.Single(p => p.LogID);

              //}
              //HomeWindow homeWin = new HomeWindow();
              //homeWin.Show();
              //this.Close();
          }
      }
    }
}
